package com.situ.stmall.mapper;

import com.situ.stmall.entity.GoodsPicturre;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface GoodsPicturreMapper {
    @Delete({
        "delete from goods_picturre",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into goods_picturre (id, goods_id, ",
        "name)",
        "values (#{id,jdbcType=INTEGER}, #{goodsId,jdbcType=INTEGER}, ",
        "#{name,jdbcType=VARCHAR})"
    })
    int insert(GoodsPicturre record);

    @Select({
        "select",
        "id, goods_id, name",
        "from goods_picturre",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_id", property="goodsId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR)
    })
    GoodsPicturre selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, goods_id, name",
        "from goods_picturre"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_id", property="goodsId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR)
    })
    List<GoodsPicturre> selectAll();

    @Update({
        "update goods_picturre",
        "set goods_id = #{goodsId,jdbcType=INTEGER},",
          "name = #{name,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(GoodsPicturre record);

    /**
     * 批量添加
     * @param id
     * @param pictures
     * @return
     */
    @Insert({
    	"<script>",
    	"insert into goods_picturre (goods_id, name) values ",
    	"<foreach collection='pictures' item='pic' separator=','>",
    	"(#{id}, #{pic.name})",
    	"</foreach>",
    	"</script>"
    })
	int batchInsert(@Param("id")Integer id, 
			@Param("pictures")List<GoodsPicturre> pictures);
	
    @Select("select * from goods_picturre where goods_id=#{goodsId}")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="goods_id", property="goodsId", jdbcType=JdbcType.INTEGER),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR)
    })
	List<GoodsPicturre> selectByGoodsId(Integer goodsId);

    @Delete("delete from goods_picturre where goods_id=#{goodsId}")
	int deleteByGoodsId(Integer goodsId);
}